﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Common Core Extensions
//===============================================================================

using System;
using System.ComponentModel;

namespace EntLibContrib.Common.Configuration.Design.Editors
{
	/// <summary>
	/// Provides a user interface for selecting a file resource name with a specific filter.
	/// </summary>
	/// <remarks>This editor uses a <see cref="FileFilterAttribute"/> to get its file filter from</remarks>
	/// <seealso cref="FilteredFileNameEditor"/>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2117:AptcaTypesShouldOnlyExtendAptcaBaseTypes")]
	public abstract class FileResourceNameEditor : FilteredFileNameEditor
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="FileResourceNameEditor"/> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
		protected FileResourceNameEditor()
    {
    }
		#endregion

		#region Public Methods
		/// <summary>
		/// Edits the specified object using the editor style provided by the GetEditStyle method.
		/// </summary>
		/// <param name="context">An <para>ITypeDescriptorContext</para> that can be used to gain additional context information.</param>
		/// <param name="provider">A service provider object through which editing services may be obtained.</param>
		/// <param name="value">An instance of the value being edited.</param>
		/// <returns>
		/// The new value of the object. If the value of the object hasn't changed, this should return the same object it was passed.
		/// </returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			IFileResourceName fileResourceName = value as IFileResourceName;

			if (fileResourceName == null)
				return value;

			string fileName = (string)FindTypeConverter().ConvertTo(fileResourceName, typeof(string));
			string returnFileName = (string)base.EditValue(context, provider, fileName);

			return (IFileResourceName)FindTypeConverter().ConvertFrom(returnFileName);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Finds the type converter.
		/// </summary>
		/// <returns></returns>
		protected abstract TypeConverter FindTypeConverter();
		#endregion
	}
}
